package com.moyq5.permit.center.feign.server.controller;

import java.util.List;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.converter.json.MappingJacksonValue;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PatchMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

import com.moyq5.mvc.framework.general.Body;
import com.moyq5.mvc.framework.general.Page;
import com.moyq5.mvc.framework.general.Paging;
import com.moyq5.mvc.framework.general.Sorting;
import com.moyq5.mvc.framework.jackson.result.ListResult;
import com.moyq5.mvc.framework.jackson.result.ObjectResult;
import com.moyq5.mvc.framework.jackson.result.PageResult;
import com.moyq5.mvc.framework.jackson.result.StatusResult;
import com.moyq5.mvc.framework.utils.EntityUtils;
import com.moyq5.mvc.framework.utils.StringUtils;
import com.moyq5.permit.center.bean.entity.SysRole;
import com.moyq5.permit.center.bean.search.SysRole4Search;
import com.moyq5.permit.center.core.ServiceErrorCode;
import com.moyq5.permit.center.core.ServiceOpenException;
import com.moyq5.permit.center.core.service.SysRoleService;
import com.moyq5.permit.center.feign.server.BaseController;
import com.moyq5.permit.center.feign.server.jackson.filter.SysRoleFilter;


/**
 * @author Moyq5’s Tools
 * @since 2019-07-16 11:24:08
 */
@RestController
@RequestMapping("/role")
public class SysRoleController extends BaseController {
	
	private static Logger log = LoggerFactory.getLogger(SysRoleController.class);
	
	@Autowired
	private SysRoleService roleService;
	
	/**
	 * 根据ID获取对象
	 * @author Moyq5’s Tools
	 * @param roleId
	 * @param filter
	 * @return
	 */
	@PostMapping("/oneById")
	@ResponseBody
	public MappingJacksonValue oneById(@RequestBody Long roleId, String filter) {
		log.debug(">>>>oneById: roleId={},filter={}", roleId, filter);
		SysRole role = roleService.one(roleId);
		ObjectResult<SysRole> objRst = new ObjectResult<SysRole>();
		objRst.setData(role);
		objRst.setOk();
		MappingJacksonValue value = new MappingJacksonValue(objRst);
		value.setFilters(SysRoleFilter.get(filter));
		return value;
	}
	
	/**
	 * 查询记录数
	 * @author Moyq5’s Tools
	 * @param role4s
	 * @return
	 */
	@PostMapping("/count")
	@ResponseBody
	public ObjectResult<Long> count(@RequestBody Body<SysRole4Search, String> role4s) {
		log.debug(">>>>count: ");
		Long count = roleService.count(role4s.getEntity(), role4s.getCustom());
		ObjectResult<Long> objRst = new ObjectResult<Long>();
		objRst.setData(count);
		objRst.setOk();
		return objRst;
	}
	
	/**
	 * 查询单个对象
	 * @author Moyq5’s Tools
	 * @param role4s
	 * @param filter
	 * @return
	 */
	@PostMapping("/one")
	@ResponseBody
	public MappingJacksonValue one(@RequestBody Body<SysRole4Search, Sorting> role4s, String filter) {
		log.debug(">>>>one: filter={}", filter);
		SysRole role = roleService.one(role4s.getEntity(), role4s.getCustom());
		ObjectResult<SysRole> objRst = new ObjectResult<SysRole>();
		objRst.setData(role);
		objRst.setOk();
		MappingJacksonValue value = new MappingJacksonValue(objRst);
		value.setFilters(SysRoleFilter.get(filter));
		return value;
	}
	
	/**
	 * 列表查询
	 * @author Moyq5’s Tools
	 * @param role4s
	 * @param filter
	 * @return
	 */
	@PostMapping("/list")
	@ResponseBody
	public MappingJacksonValue list(@RequestBody Body<SysRole4Search, Sorting> role4s, String filter) {
		log.debug(">>>>list: filter={}", filter);
		List<SysRole> list = roleService.list(role4s.getEntity(), role4s.getCustom());
		ListResult<SysRole> listRst = new ListResult<SysRole>();
		listRst.setData(list);
		listRst.setOk();
		MappingJacksonValue value = new MappingJacksonValue(listRst);
		value.setFilters(SysRoleFilter.get(filter));
		return value;
	}
	
	/**
	 * 分页查询
	 * @author Moyq5’s Tools
	 * @param role4s
	 * @param filter
	 * @return
	 */
	@PostMapping("/page")
	@ResponseBody
	public MappingJacksonValue page(@RequestBody Body<SysRole4Search, Paging> role4s, String filter) {
		log.debug(">>>>page: filter={}", filter);
		Page<SysRole> page = roleService.page(role4s.getEntity(), role4s.getCustom());
		PageResult<SysRole> pageRst = new PageResult<SysRole>();
		pageRst.setData(page);
		pageRst.setOk();
		MappingJacksonValue value = new MappingJacksonValue(pageRst);
		value.setFilters(SysRoleFilter.get(filter));
		return value;
	}
	
	/**
	 * 添加
	 * @author Moyq5’s Tools
	 * @param from
	 * @return
	 */
	@PutMapping
	@ResponseBody
	public ObjectResult<Long> create(@RequestBody SysRole from) {
		log.debug(">>>>add: SysRole");
		if (StringUtils.isEmpty(from.getCode())) {
			from.setCode("rc_" + from.getPlatform().name().toLowerCase() + "_" + StringUtils.random(8));
		}
		SysRole4Search role4s = new SysRole4Search();
		role4s.setPlatform(from.getPlatform());
		role4s.setCode(from.getCode());
		if (roleService.count(role4s) > 0) {
			throw new ServiceOpenException(ServiceErrorCode.C0001);
		}
		SysRole role = roleService.persist(from);
		ObjectResult<Long> result = new ObjectResult<Long>();
		result.setData(role.getRoleId());
		result.setOk();
		return result;
	}
	
	/**
	 * 更新
	 * @author Moyq5’s Tools
	 * @param form
	 * @return
	 */
	@PatchMapping
	@ResponseBody
	public StatusResult update(@RequestBody SysRole form) {
		log.debug(">>>>update: SysRole");
		SysRole role = roleService.one(form.getRoleId());
		EntityUtils.combine(form, role);
		roleService.merge(role);
		StatusResult result = new StatusResult();
		result.setOk();
		return result;
	}
	
	/**
	 * 删除
	 * @author Moyq5’s Tools
	 * @param roleId
	 * @return
	 */
	@DeleteMapping
	@ResponseBody
	public StatusResult delete(@RequestBody Long roleId) {
		log.debug(">>>>delete: roleId={}", roleId);
		roleService.remove(roleId);
		StatusResult result = new StatusResult();
		result.setOk();
		return result;
	}
	
}
